import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class Question1 {
    public static void main(String[] args) throws SQLException {
        //1. 新增貂蝉同学的借阅记录：诗经，从2019年9月25日17:50到2019年10月25日17:50
        DataSource dataSource=new MysqlDataSource();
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/ebook?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("123456");
        Connection connection= dataSource.getConnection();
        String sql="insert into borrow_info (book_id,student_id,start_time,end_time) values ((select id from book where name='诗经'),(select id from student where name='貂蝉'),'2019-09-25 17:50:00' ,'2019-10-25 17:50:00')";
        PreparedStatement statement= connection.prepareStatement(sql);
        int n =statement.executeUpdate();
        System.out.println("n="+n);
        statement.close();
        connection.close();

    }
}
